<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                template="./../template/WorkspaceTemplate.xhtml">

    <ui:define name="top">

    </ui:define>

    <ui:define name="left">

    </ui:define>

    <ui:define name="right">

    </ui:define>

    <ui:define name="content">

        <h:form id="createGoodsReceiptForm" style="font-size: 85%">
            <h2>Create Goods Receipt</h2>
            <br/>
            <p:panel>
                <p:messages id="messages"/>
                <br/>
                <h:panelGrid columns ="6" cellpadding="10">
                    <h:outputLabel value="Sales Order Reference: " style="font-weight: bold"/>
                    <p:inputText id="soNumber" value="#{grManagerBean.gr.rmso.salesOrderId}" disabled="true"/>
                    <p:commandButton update="viewSOForm" oncomplete="soDialog.show();" image="ui-icon ui-icon-search" />
                    <h:outputText value=""/>
                    <h:outputLabel value="Goods Receipt Id: " style="font-weight: bold"/>
                    <h:outputText value="#{grManagerBean.gr.goodsReceiptId}"/>

                    <h:outputLabel value="Supplier Id: " style="font-weight: bold"/>
                    <h:outputText id="supplierId" value="#{grManagerBean.gr.supplier.supplierId}" />
                    <h:outputText value=""/>
                    <h:outputText value=""/>
                    <h:outputLabel value="Order Date: " style="font-weight: bold"/>
                    <h:outputText id="date" value="#{grManagerBean.gr.dateReceipt}">
                        <f:convertDateTime pattern="MMMM dd, yyyy"/>
                    </h:outputText>

                    <h:outputLabel value="Supplier Name: " style="font-weight: bold"/>
                    <h:outputText id="supplierName" value="#{grManagerBean.gr.supplier.supplierName}" />

                </h:panelGrid>

                <br/>
                <br/>
                <br/>
                <h:outputText value="Raw Material Items" style="font-weight: bold; font-size: 110%"/>
                <p:separator/> 
                <br/>
                <p:dataTable id="items" var="rec" rowIndexVar="row" value="#{grManagerBean.gr.itemlines}" emptyMessage="No items added.">
                    <p:column headerText="S/N" >
                        <h:outputText value="#{row + 1}"/>
                    </p:column>

                    <p:column headerText="Raw Material Item">
                        <h:outputText value="#{rec.rawMaterialSupplyInformation.rawMaterial.name}"/>
                    </p:column>

                    <p:column headerText="Quantity (Lot Number)">
                        <h:outputText value="#{rec.qty}"/>
                    </p:column>
                    

                    <p:column headerText="Options">
                        <p:commandButton title="Edit" image="ui-icon ui-icon-pencil" actionListener="#{grManagerBean.editItemLine}" update="editItemForm" oncomplete="editItemDialog.show()">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                        <p:commandButton title="Remove" image="ui-icon ui-icon-trash" actionListener="#{grManagerBean.removeItemLine}" oncomplete="confirmation.show()">
                            <f:attribute name="selected" value="#{rec}"/>
                        </p:commandButton>
                    </p:column>

                </p:dataTable>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Add Raw Material Items" image="ui-icon ui-icon-circle-plus" oncomplete="orderItemDialog.show()"/>
                </div>
                <br/>
                <br/>
                <br/>
                
            </p:panel>

            <br/>
            <br/>
            <div align="right">
                <p:commandButton image="ui-icon ui-icon-disk" value="Create Goods Receipt" action="#{grManagerBean.createGoodsReceipt()}"  update="createGoodsReceiptForm:messages"/>
            </div>
            <br/>
            <br/>
        </h:form>

        <p:dialog header="Search Sales Order Without Goods Receipt" modal="true" widgetVar="soDialog" hideEffect="explode" height="600" width="800">
            <h:form id="soForm">
                <p:dataTable var="so" value="#{grManagerBean.allSalesOrderWOGoodsReceipt}"
                             emptyMessage="No Sales Order found with given criteria" selection="#{grManagerBean.gr.rmso}" selectionMode="single">

                    <p:column filterBy="#{so.salesOrderId}" 
                              headerText="Invoice No"
                              filterMatchMode="contains">
                        <h:outputText value="#{so.salesOrderId}" />
                    </p:column>

                    <p:column filterBy="#{so.supplier.supplierName}" 
                              headerText="Supplier Name"
                              filterMatchMode="contains">
                        <h:outputText value="#{so.supplier.supplierName}" />
                    </p:column>

                    <p:column filterBy="#{so.salesOrderDate}" 
                              headerText="Date Received"
                              filterMatchMode="contains">
                        <h:outputText value="#{so.salesOrderDate}">
                            <f:convertDateTime pattern="MMMM dd, yyyy"/>
                        </h:outputText>
                    </p:column>

                </p:dataTable>
                <br/>
                <p:commandButton value="Select Sales Order" actionListener="#{grManagerBean.setSalesOrder}" update="createGoodsReceiptForm:soNumber createGoodsReceiptForm:supplierId createGoodsReceiptForm:supplierName createGoodsReceiptForm:date createGoodsReceiptForm:items rawMaterialForm orderItemForm editItemForm" oncomplete="soDialog.hide()"/>
            </h:form>
        </p:dialog>

        <p:dialog id="orderItemDialog" header="Add Order Items" widgetVar="orderItemDialog" showEffect="fade" hideEffect="fade" resizable="false" modal="true" width="600" closable="false">  
            <h:form id="orderItemForm">
                <br/>
                <p:messages/>
                <h:panelGrid columns="3" cellpadding="10">
                    <h:outputLabel style="font-weight: bold" value="Raw Material Item: "/>
                    <p:inputText id ="rawMaterial" size="40" value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.name}" disabled="true"/>
                    <p:commandButton oncomplete="rawMaterialDialog.show()" image="ui-icon ui-icon-search" title="Get Raw Material" update="rawMaterialForm"/>
                    
                    <h:outputLabel style="font-weight: bold" value="Lot Size: "/>
                    <h:outputText value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.lotSize}"/>
                    <h:outputLabel value=""/>
                    
                    <h:outputLabel style="font-weight: bold" value="Lead Time: "/>
                    <h:outputText value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.leadtime}"/>
                    <h:outputLabel value=""/>

                    <h:outputLabel style="font-weight: bold" value="Quantity: "/>
                    <p:inputText value="#{grManagerBean.rmil.qty}"/>
                    <h:outputLabel value=""/>
                </h:panelGrid>

                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Cancel" actionListener="#{grManagerBean.cancelAddItemLine}" update="createGoodsReceiptForm:poNumber createGoodsReceiptForm:supplierId createGoodsReceiptForm:supplierName rawMaterialForm orderItemForm editItemForm" oncomplete="orderItemDialog.hide()"/>
                    <p:commandButton value="Add Item" update="createGoodsReceiptForm:items rawMaterialForm orderItemForm editItemForm" actionListener="#{grManagerBean.addItemLine}" oncomplete="handleAddOrderItem(xhr, status, args)"/>
                </div>
                <br/>

            </h:form>
        </p:dialog>

        <script type="text/javascript">  
            function handleAddOrderItem(xhr, status, args) {
                if(!args.valid) {  
                    jQuery('#orderItemDialog').parent().effect("shake", { times:3 }, 100);
                } else {
                    orderItemDialog.hide();
                }
            }
        </script>

        <p:dialog header="Search Raw Material" widgetVar="rawMaterialDialog" showEffect="fade" hideEffect="fade" resizable="false" modal="true" width="600">
            <h:form id="rawMaterialForm">
                <br/>
                <p:dataTable var="rawMaterial" value="#{grManagerBean.supplierRawMaterial}" emptyMessage="No raw material found with given criteria"  selection="#{grManagerBean.rmil.rawMaterialSupplyInformation}" selectionMode="single">

                    <p:column filterBy="#{rawMaterial.rawMaterial.name}" 
                              headerText="Raw Material"
                              filterMatchMode="contains">
                        <h:outputText value="#{rawMaterial.rawMaterial.name}" />
                    </p:column>

                    <p:column filterBy="#{rawMaterial.rawMaterial.lotSize}" 
                              headerText="Lot Size"
                              filterMatchMode="contains">
                        <h:outputText value="#{rawMaterial.rawMaterial.lotSize}" />
                    </p:column>

                    <p:column filterBy="#{rawMaterial.rawMaterial.leadtime}" 
                              headerText="Lead Time"
                              filterMatchMode="contains">
                        <h:outputText value="#{rawMaterial.rawMaterial.leadtime}" />
                    </p:column>

                </p:dataTable>
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Select Raw Material" update="orderItemForm editItemForm" oncomplete="rawMaterialDialog.hide()"/>
                </div>
                <br/>
            </h:form>
        </p:dialog>
        
        <p:dialog id="editItemDialog" header="Edit Order Items" widgetVar="editItemDialog" showEffect="fade" hideEffect="fade" resizable="false" modal="true" width="600" closable="false">  
            <h:form id="editItemForm">
                <br/>
                <p:messages/>
                <h:panelGrid columns="3" cellpadding="10">
                    <h:outputLabel style="font-weight: bold" value="Raw Material Item: "/>
                    <p:inputText size="40" value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.name}" disabled="true"/>
                    <p:commandButton oncomplete="rawMaterialDialog.show()" image="ui-icon ui-icon-search" title="Get Raw Material" update="rawMaterialForm"/>
                    
                    <h:outputLabel style="font-weight: bold" value="Lot Size: "/>
                    <h:outputText value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.lotSize}"/>
                    <h:outputLabel value=""/>
                    
                    <h:outputLabel style="font-weight: bold" value="Lead Time: "/>
                    <h:outputText value="#{grManagerBean.rmil.rawMaterialSupplyInformation.rawMaterial.leadtime}"/>
                    <h:outputLabel value=""/>
                    
                    <h:outputLabel style="font-weight: bold" value="Quantity (Lot Number): "/>
                    <p:inputText value="#{grManagerBean.rmil.qty}"/>
                    <h:outputLabel value=""/>
                </h:panelGrid>
                
                <br/>
                <br/>
                <div align="right">
                    <p:commandButton value="Cancel" update="createGoodsReceiptForm:items rawMaterialForm orderItemForm editItemForm" actionListener="#{grManagerBean.cancelEditItemLine}" oncomplete="editItemDialog.hide()"/>
                    <p:commandButton value="Update Item" update="createGoodsReceiptForm:items rawMaterialForm orderItemForm editItemForm" actionListener="#{grManagerBean.updateItemLine}" oncomplete="handleEditOrderItem(xhr, status, args)"/>
                </div>
                <br/>
                
            </h:form>
        </p:dialog>
        
        <script type="text/javascript">  
            function handleEditOrderItem(xhr, status, args) {
                if(!args.valid) {  
                    jQuery('#editItemDialog').parent().effect("shake", { times:3 }, 100);
                } else {
                    editItemDialog.hide();
                }
            }
        </script>
        
        <p:confirmDialog message="Delete item line?" width="200"  
                         showEffect="fade" hideEffect="fade"  
                         header="Confirm" severity="alert" widgetVar="confirmation">  
            <h:form id="confirm">
                <p:commandButton value="Yes" actionListener="#{grManagerBean.confirmRemoveItemLine}" update="createGoodsReceiptForm:items rawMaterialForm orderItemForm editItemForm" oncomplete="confirmation.hide()"/>  
                <p:commandButton value="No" actionListener="#{grManagerBean.cancelRemoveItemLine}" oncomplete="confirmation.hide()"/>  
            </h:form>
        </p:confirmDialog>
        
    </ui:define>
    
    

    <ui:define name="bottom">

    </ui:define>

</ui:composition>
